package com.sandasai.action.student;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.UriUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.URLUtil;
import com.sandasai.service.StudentManager;
import com.sandasai.vo.WorkBean;
@Controller
@Scope("prototype")
public class SubmitWorkAction extends ActionSupport {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String title;
    private String workType;
    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    private String savePath;
    @Resource
	private StudentManager studentManager;
    
    
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getWorkType() {
        return workType;
    }
    public void setWorkType(String workType) {
        this.workType = workType;
    }
    public File getUpload() {
        return upload;
    }
    public void setUpload(File upload) {
        this.upload = upload;
    }
    public String getUploadContentType() {
        return uploadContentType;
    }
    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    public String getUploadFileName() {
        return uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
    public String getSavePath() {
        return savePath;
    }
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
    /**
     * @return
     */
    private String tips;
    
    public String execute() throws Exception{
        ActionContext ctx = ActionContext.getContext();
        String realPath = ServletActionContext.getServletContext().getRealPath(this.getSavePath());
        //创建保存上传文件的文件夹
        File uploadFolder = new File(realPath);
        if(!uploadFolder.exists() && !uploadFolder.isDirectory()) {
        	uploadFolder.mkdirs();
        }
        String uniqueFileName = UUID.randomUUID().toString() + getUploadFileName().substring(getUploadFileName().lastIndexOf("."));
        FileOutputStream fos = new FileOutputStream(realPath + "\\" + uniqueFileName);
        FileInputStream fis = new FileInputStream(getUpload());
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = fis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        WorkBean workBean = new WorkBean();
        workBean.setOwner((String)(ctx.getSession().get("username")));
        workBean.setTitle(this.getTitle());
        workBean.setType(this.getWorkType());
        workBean.setFilePath(uniqueFileName);
        
        if(studentManager.addWork(workBean)) {
        	this.tips = UriUtils.encodeQuery("上传成功！", "utf-8");
        	//this.tips = "上传成功！";
            //ctx.put("tips", "上传成功！");
        }else {
        	this.tips = UriUtils.encodeQuery("上传成功！", "utf-8");
//        	this.tips = "上传失败！";
//            ctx.put("tips", "上传失败！");
        }
        return SUCCESS;
    }
	public String getTips() {
		return tips;
	}
	public void setTips(String tips) {
		this.tips = tips;
	}
}